package com.system.function.service;

import com.system.Tools.TimeTools;
import com.system.function.dao.CommodityDAO;
import com.system.function.object.Commodity;
import com.system.function.object.Supplier;
import com.system.function.object.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityService {
    @Autowired
    private CommodityDAO CommodityDAO;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private TimeTools timeTools;


    //查询全部
    public List<Commodity> getAllCommodity(Commodity Commodity) {
        List<Commodity> allCommodity = new ArrayList<>();
        try {
            List<Commodity> allCommoditySearch = CommodityDAO.getAllCommodity(Commodity);
            for (Commodity commoditySearch : allCommoditySearch) {

                //查询仓库
                Warehouse warehouse = new Warehouse();
                warehouse.setId(commoditySearch.getWarehouseId());
                Warehouse warehouseSearch = warehouseService.getWarehouse(warehouse);
                commoditySearch.setWarehouse(warehouseSearch);

                //查询供应商
                Supplier supplier = new Supplier();
                supplier.setId(commoditySearch.getSupplierId());
                Supplier supplierSearch = supplierService.getSupplier(supplier);
                commoditySearch.setSupplier(supplierSearch);

                //比较保质期是否是正常的
                String saveTime = commoditySearch.getSaveTime();
                String warrantyTime = commoditySearch.getWarrantyTime();
                String overdue = timeTools.isOverdue(saveTime, Integer.parseInt(warrantyTime));

                String stock = commoditySearch.getStock();
                int i = Integer.parseInt(stock);
                if(i < 200){
                    commoditySearch.setStockState("库存不足,小于200");
                }
                commoditySearch.setState(overdue);

                allCommodity.add(commoditySearch);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allCommodity;
    }

    //查询全部
    public List<Commodity> groupByWarehouse(Commodity Commodity) {
        List<Commodity> allCommodity = new ArrayList<>();
        try {
            List<Commodity> allCommoditySearch = CommodityDAO.groupByWarehouse(Commodity);
            for (Commodity commoditySearch : allCommoditySearch) {

                //查询仓库
                Warehouse warehouse = new Warehouse();
                warehouse.setId(commoditySearch.getWarehouseId());
                Warehouse warehouseSearch = warehouseService.getWarehouse(warehouse);
                commoditySearch.setWarehouse(warehouseSearch);

                allCommodity.add(commoditySearch);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allCommodity;
    }

    //查询全部
    public List<Commodity> groupBySupplier(Commodity Commodity) {
        List<Commodity> allCommodity = new ArrayList<>();
        try {
            List<Commodity> allCommoditySearch = CommodityDAO.groupBySupplier(Commodity);
            for (Commodity commoditySearch : allCommoditySearch) {

                //查询供应商
                Supplier supplier = new Supplier();
                supplier.setId(commoditySearch.getSupplierId());
                Supplier supplierSearch = supplierService.getSupplier(supplier);
                commoditySearch.setSupplier(supplierSearch);

                allCommodity.add(commoditySearch);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allCommodity;
    }

    //查询全部
    public List<Commodity> groupByRemarks(Commodity Commodity) {
        List<Commodity> allCommodity = new ArrayList<>();
        try {
            allCommodity = CommodityDAO.groupByRemarks(Commodity);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return allCommodity;
    }

    //查询总数
    public Integer getCountCommodity(Commodity Commodity) {
        return CommodityDAO.getCountCommodity(Commodity);
    }

    //查询总数
    public Integer countSUmCommodity(Commodity Commodity) {
        return CommodityDAO.countSumCommodity(Commodity);
    }

    //查询
    public Commodity getCommodity(Commodity Commodity) {
        Commodity CommoditySearch = null;
        try {
            CommoditySearch = CommodityDAO.getCommodity(Commodity);
            if(null != CommoditySearch){
                //查询仓库
                Warehouse warehouse = new Warehouse();
                warehouse.setId(CommoditySearch.getWarehouseId());
                Warehouse warehouseSearch = warehouseService.getWarehouse(warehouse);
                CommoditySearch.setWarehouse(warehouseSearch);

                //查询供应商
                Supplier supplier = new Supplier();
                supplier.setId(CommoditySearch.getSupplierId());
                Supplier supplierSearch = supplierService.getSupplier(supplier);
                CommoditySearch.setSupplier(supplierSearch);
            }

        } catch (Exception e) {
            //e.printStackTrace();
        }
        return CommoditySearch;
    }

    //增加
    public String addCommodity(Commodity Commodity) {
        String isSuccess = "false";
        try {
            Commodity.setId(timeTools.getTimeInt());
            Commodity.setStock("0");
            Commodity.setPurchase("0");

            //添加时间
            if(null == Commodity.getSaveTime()){
                String timeYMD = timeTools.getTimeYMD();
                Commodity.setSaveTime(timeYMD);
            }else{
                try{
                    String[] split = Commodity.getSaveTime().split("/");
                    Commodity.setSaveTime(split[2]+"-"+split[0]+"-"+split[1]);
                }catch (Exception e){}

            }

            int resultNum = CommodityDAO.addCommodity(Commodity);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }

    //删除
    public String delCommodity(Commodity Commodity) {
        String isSuccess = "false";
        try {
            //查询当前仓库下是否存在商品

            int resultNum = CommodityDAO.delCommodity(Commodity);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    //修改
    public String updateCommodity(Commodity Commodity) {
        String isSuccess = "false";
        try {
            int resultNum = CommodityDAO.updateCommodity(Commodity);
            if (resultNum > 0) {
                isSuccess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }


}
